<template>
  <div>
    <!--顶部搜索栏-->
    合同号:
    <el-input
        v-model="fuzzy.barcode"
        style="width: 140px"
        size="small"
    ></el-input>
    客户:
    <el-input v-model="fuzzy.name" style="width: 140px" size="small"></el-input>
    类型:
    <el-select v-model="fuzzy.type" placeholder="全部" size="small" clearable>
      <el-option label="省外" value="1"></el-option>
      <el-option label="省内" value="0"></el-option>
    </el-select>
    创建日期:
    <el-date-picker
        v-model="fuzzy.startTime"
        type="date"
        placeholder="最早"
        value-format="yyyy-MM-dd"
    >
    </el-date-picker
    >-
    <el-date-picker
        v-model="fuzzy.endTime"
        type="date"
        placeholder="最晚"
        value-format="yyyy-MM-dd"
    >
    </el-date-picker>
    <br />
    <el-button
        type="primary"
        size="default"
        @click="handleSearch"
        style="margin-left: 10px"
    >搜索</el-button
    >
    <el-button
        type="primary"
        size="default"
        @click="handleAdd"
        style="margin-left: 10px"
    >新增</el-button
    ><br />
    <!--表格-->
    <el-table :data="contracts" style="width: 930px" stripe>
      <el-table-column label="序号" width="60" align="center">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
          prop="barcode"
          label="合同编号"
          width="120"
          align="center"
      >
        <template #default="scope">
          <div
              @click="handleShowDetail(scope.row.contractid)"
              style="cursor: pointer; color: skyblue"
          >
            {{ scope.row.barcode }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="客户" width="120" align="center">
      </el-table-column>
      <el-table-column prop="type" label="类型" width="120" align="center">
        <template #default="scope">
          {{ scope.row.type === 0 ? "省内" : "省外" }}
        </template>
      </el-table-column>
      <el-table-column
          prop="createtime"
          label="创建日期"
          width="240"
          align="center"
      >
      </el-table-column>
      <el-table-column label="操作" width="270" align="center">
        <template #default="scope">
          <el-button
              type="primary"
              size="default"
              style="margin-right: 5px"
              @click="handleEdit()"
          >
            编辑
          </el-button>
          <el-popconfirm
              title="确定要删除吗？"
              @onConfirm="handleDelete(scope.row.contractid)"
          >
            <template #reference>
              <el-button
                  type="danger"
                  size="default"
                  style="margin-right: 5px"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
    >
    </el-pagination>
    <!--新增和编辑对话框-->
    <AddOrEditContract
        ref="AddOrEditDialogRef"
        @refresh="refresh"
    ></AddOrEditContract>
    <!--合同详情-->
    <ContractDetail ref="ContractDetailRef"></ContractDetail>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue';
import { findPages, deleteOne, findPagesByKey } from '../services/Contract.ts';
import AddOrEditContract from '../components/AddOrEditContract.vue';
import ContractDetail from '../components/ContractDetail.vue';

interface FuzzySearch {
  barcode?: string;
  name?: string;
  type?: string;
  startTime?: string;
  endTime?: string;
  pageSize?: number;
  pageNum?: number;
}

export default defineComponent({
  name: 'Contract',
  components: { AddOrEditContract, ContractDetail },

  setup() {
    const fuzzy = ref<FuzzySearch>({});
    const pageNum = ref(1);
    const pageSize = ref(7);
    const contracts = ref<any[]>([]);
    const total = ref(1);
    const isFuzzy = ref(false);

    const AddOrEditDialogRef = ref(null);
    const ContractDetailRef = ref(null);

    onMounted(async () => {
      const response = await findPages(pageNum.value, pageSize.value);
      contracts.value = response.data.data.rows;
      total.value = response.data.data.total;
      console.log(AddOrEditDialogRef.value);
    });

    const handleEdit = () => {
      (AddOrEditDialogRef.value as any).show();
    };

    const handleDelete = async (contractid: string) => {
      await deleteOne(contractid);
      (this as any).$message.success('删除合同成功');
      await refresh();
    };

    const handleCurrentChange = (newPageNum: number) => {
      pageNum.value = newPageNum;
      refresh();
    };

    const handleSearch = async () => {
      fuzzy.value.pageSize = pageSize.value;
      fuzzy.value.pageNum = pageNum.value;
      const response = await findPagesByKey(fuzzy.value);
      contracts.value = response.data.data.rows;
      total.value = response.data.data.total;
    };

    const handleAdd = () => {
      (AddOrEditDialogRef.value as any).show();
    };

    const refresh = async () => {
      if (isFuzzy.value) {
        fuzzy.value.pageSize = pageSize.value;
        fuzzy.value.pageNum = pageNum.value;
        const response = await findPagesByKey(fuzzy.value);
        contracts.value = response.data.data.rows;
        total.value = response.data.data.total;
      } else {
        const response = await findPages(pageNum.value, pageSize.value);
        contracts.value = response.data.data.rows;
        total.value = response.data.data.total;
      }
    };

    const handleShowDetail = (contractid: string) => {
      (ContractDetailRef.value as any).show(contractid);
    };

    return {
      fuzzy,
      pageNum,
      pageSize,
      contracts,
      total,
      isFuzzy,
      handleEdit,
      handleDelete,
      handleCurrentChange,
      handleSearch,
      handleAdd,
      refresh,
      handleShowDetail,
      AddOrEditDialogRef,
      ContractDetailRef,
    };
  },
});
</script>

<style lang="css" scoped></style>
